//
//  LHRadioDownloadTask.m
//  EPA
//
//  Created by 卢浩 on 16/4/27.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHRadioDownloadTask.h"
#import "LHRadioDownloadManager.h"
#import "LHRadioDetailModel.h"

@interface LHRadioDownloadTask ()<NSURLSessionDownloadDelegate>

@property (nonatomic,copy) ProgressBlock progressBlock;

@property (nonatomic,copy) CompleteBlock completeBlock;

@property (nonatomic,strong) NSString *musicUrl;

@property (nonatomic,strong) NSString *mp3Name;

@property (nonatomic,strong) NSURLSessionDownloadTask *downloadTask;

@end

@implementation LHRadioDownloadTask

//开始
- (void)resume{
    [self.downloadTask resume];
}

//暂停
- (void)suspend{
    [self.downloadTask suspend];
}

//修改block
- (void)setProgressBlock:(ProgressBlock)proBlock{
    if (_progressBlock != proBlock) {
        _progressBlock = proBlock;
    }
}

- (void)setCompleteBlock:(CompleteBlock)comBlock{
    if (_completeBlock != comBlock) {
        _completeBlock = comBlock;
    }
}

- (void)downloadUrl:(NSURL *)url ProgressBlock:(ProgressBlock)proBlock CompleteBlock:(CompleteBlock)comBlock{
    
    self.musicUrl = url.absoluteString;
    self.progressBlock = proBlock;
    self.completeBlock = comBlock;
    self.mp3Name       = [url.absoluteString stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
    self.mp3Name       = [self.mp3Name stringByReplacingOccurrencesOfString:@":" withString:@"_"];
    
    //网络请求
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url];
    self.downloadTask = downloadTask;
    [self.downloadTask resume];
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
    //下载进度
    float progressValue = totalBytesWritten * 1.0 / totalBytesExpectedToWrite;
    //回调参数
    if (self.progressBlock) {
        self.progressBlock(progressValue);
    }
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
    
    
    
    //caches文件夹路径
    NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    
    //移动后的路径
    NSString *filePath = [cachesPath stringByAppendingPathComponent:self.mp3Name];
    
    //移动
    [[NSFileManager defaultManager] moveItemAtPath:[location path] toPath:filePath error:nil];
    
    NSLog(@"下载成功!");
    
    if (self.completeBlock) {
        self.completeBlock(filePath);
        return;
    }
    
    [self removeTask];
    
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"downloadReloadData" object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"didDownloadReloadData" object:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:[NSString stringWithFormat:@"%@downloadFinsh",self.mp3Name] object:nil];
}

- (void)removeTask{
    DownloadManagerTask *task;
    for (DownloadManagerTask *tempTask in [LHRadioDownloadManager shareManager].array) {
        if ([tempTask.model.musicUrl isEqualToString:self.musicUrl]) {
            task = tempTask;
        }
    }
    [[LHRadioDownloadManager shareManager].array removeObject:task];
}


@end
